/**
 * 
 */
package valverde.nacho.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import valverde.nacho.dao.ContactDao;
import valverde.nacho.domain.Contact;

/**
 * @author ignacio.canon@entelgy.com
 */
public class MockContactDao implements ContactDao {
    private static Map<Integer, Contact> membbdd = new HashMap<Integer, Contact>();

    /** instance */
    private static MockContactDao instance = new MockContactDao();

    /**
     * @return
     */
    public static MockContactDao getInstance() {
	return instance;
    }

    private MockContactDao() {
	Contact c = new Contact();
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.YEAR, -30);
	c.setBirthDate(cal.getTime());
	c.setEmail("ignacio.canon@gmail.com");
	c.setFirstName("Ignacio");
	c.setLastName("Cañon");
	c.setPhoneNumber("666555444");
	save(c);
	c = new Contact();
	cal.add(Calendar.YEAR, -10);
	c.setBirthDate(cal.getTime());
	c.setEmail("manolo.cosa@gmail.com");
	c.setFirstName("Manolo");
	c.setLastName("Cosa");
	c.setPhoneNumber("111222555");
	save(c);
    }

    /*
     * (non-Javadoc)
     * 
     * @see valverde.nacho.dao.ContactDao#read()
     */
    public List<Contact> read() {
	return new ArrayList<Contact>(membbdd.values());
    }

    /*
     * (non-Javadoc)
     * 
     * @see valverde.nacho.dao.ContactDao#read(java.lang.Integer)
     */
    public Contact read(Integer id) {
	return id != null ? membbdd.get(id) : null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see valverde.nacho.dao.ContactDao#save(valverde.nacho.domain.Contact)
     */
    public void save(Contact contact) {
	Integer id = (int) (Math.random() * 1000);
	while (membbdd.containsKey(id)) {
	    id = (int) Math.random();
	}
	contact.setId(id);
	membbdd.put(id, contact);

    }

    /*
     * (non-Javadoc)
     * 
     * @see valverde.nacho.dao.ContactDao#delete(java.lang.Integer)
     */
    public void delete(Integer id) {
	membbdd.remove(id);
    }

}
